package com.yl.chainStore.util

import java.util.*

/**
 * 出生日期计算年龄
 */
fun getAge(birthDay:Date):Int?{
	val cal = Calendar.getInstance()
	if (cal.before(birthDay)) {//出生日期晚于当前时间，无法计算
		return null
	}
	val yearNow = cal.get(Calendar.YEAR)
	val monthNow = cal.get(Calendar.MONTH)
	val dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH)
	cal.time = birthDay
	val yearBirth = cal.get(Calendar.YEAR)
	val monthBirth = cal.get(Calendar.MONTH)
	val dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH)
	var age = yearNow - yearBirth
	if (monthNow <= monthBirth) {
		if (monthNow == monthBirth) {
			if (dayOfMonthNow < dayOfMonthBirth){
				age--
			}
		}else{
			age--
		}
	}
	return age
}
